home *** CD-ROM | disk | FTP | other *** search
- // SlsDemo.cpp : Defines the class behaviors for the application.
- //
-
- #include "stdafx.h"
- #include "SlsDemo.h"
-
- #include "MainFrm.h"
- #include "SlsDemoDoc.h"
- #include "SlsDemoView.h"
- #include "ExportDlg.h"
-
- #include "SlsApi.h"
-
- #include "RegisterDlg.h"
- #include "RenewDlg.h"
- #include "ExceedDlg.h"
- #include "LicenceDlg.h"
-
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
-
- /////////////////////////////////////////////////////////////////////////////
- // CSlsDemoApp
-
- char g_szProductID[]={"9758-3050-1918-9292-6466"};
- SLS_SECRET g_arySecrets[]=
- {
- {0x76,0x3E,0x6D,0x36,0x14,0x14,0x6D,0xB8,0x1D,0xCA,0xF3,0xD5,0x34,0x2D,0x08,0xBC,0x14,0x31},
- {0x00,0xB7,0x17,0x00,0x43,0xFC,0xC0,0x7F,0x8F,0xA2,0x22,0xD2,0x1B,0x62,0xB3,0xDE,0x95,0x3F},
- {0xAC,0x2E,0x39,0x6F,0x45,0xD5,0x41,0x11,0x43,0x41,0x2B,0x75,0x9C,0xB2,0x67,0x6E,0x14,0x02},
- {0x79,0x52,0x9A,0x49,0x97,0x8B,0x1C,0x15,0x64,0xB7,0xBE,0x6C,0x7F,0x18,0xC3,0x4E,0x78,0x87},
- };
-
- /*
- SLS_SECRET g_arySecrets[4]=
- {
- {"5267-4088-9823-3212"},
- {"3166-1346-2340-4525"},
- {"2156-4924-7515-9864"},
- {"2056-0703-9264-0145"},
- };
- */
-
- BEGIN_MESSAGE_MAP(CSlsDemoApp, CWinApp)
- //{{AFX_MSG_MAP(CSlsDemoApp)
- ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
- ON_COMMAND(ID_TEST_EXPORT, OnTestExport)
- ON_COMMAND(ID_TEST_QUERYUSERINFO, OnTestQueryuserinfo)
- //}}AFX_MSG_MAP
- // Standard file based document commands
- ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew)
- ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen)
- // Standard print setup command
- ON_COMMAND(ID_FILE_PRINT_SETUP, CWinApp::OnFilePrintSetup)
- END_MESSAGE_MAP()
-
- /////////////////////////////////////////////////////////////////////////////
- // CSlsDemoApp construction
-
- CSlsDemoApp::CSlsDemoApp()
- {
- // TODO: add construction code here,
- // Place all significant initialization in InitInstance
- m_pSheriff=NULL;
- m_bRunInDemoMode=FALSE;
- m_bLicenceOK=FALSE;
- m_nAccessKey=0;
- }
-
- CSlsDemoApp::~CSlsDemoApp()
- {
- delete m_pSheriff;
- }
- /////////////////////////////////////////////////////////////////////////////
- // The one and only CSlsDemoApp object
-
- CSlsDemoApp theApp;
-
- /////////////////////////////////////////////////////////////////////////////
- // CSlsDemoApp initialization
-
- BOOL CSlsDemoApp::InitInstance()
- {
- // Standard initialization
- // If you are not using these features and wish to reduce the size
- // of your final executable, you should remove from the following
- // the specific initialization routines you do not need.
-
- #ifdef _AFXDLL
- Enable3dControls(); // Call this when using MFC in a shared DLL
- #else
- Enable3dControlsStatic(); // Call this when linking to MFC statically
- #endif
-
- // Change the registry key under which our settings are stored.
- // You should modify this string to be something appropriate
- // such as the name of your company or organization.
- SetRegistryKey(_T("Sheriff Demo"));
-
- LoadStdProfileSettings(); // Load standard INI file options (including MRU)
-
- // Register the application's document templates. Document templates
- // serve as the connection between documents, frame windows and views.
-
- CSingleDocTemplate* pDocTemplate;
- pDocTemplate = new CSingleDocTemplate(
- IDR_MAINFRAME,
- RUNTIME_CLASS(CSlsDemoDoc),
- RUNTIME_CLASS(CMainFrame), // main SDI frame window
- RUNTIME_CLASS(CSlsDemoView));
- AddDocTemplate(pDocTemplate);
-
- // Parse command line for standard shell commands, DDE, file open
- CCommandLineInfo cmdInfo;
- ParseCommandLine(cmdInfo);
- m_strUserName=cmdInfo.m_strFileName;
- if(m_strUserName.IsEmpty())
- m_strUserName="Sheriff Demo";
-
- m_pSheriff=new CSheriff(g_szProductID,m_strUserName);
- if(!m_pSheriff->Succeeded())
- {
- ShowSheriffError();
- return FALSE;
- }
- m_pSheriff->SetSecrets(g_arySecrets);
-
- if(!m_pSheriff->IsProductLicensed())
- {
- RegisterProduct();
-
- //If you want to automaticlly issue a trial licence
- //eanble the following code
-
- /* ------------------------------------------------------
- //Issue a 30-day, single-user, standalone trial licence
- SLS_LICENCE TrialLicence;
- memset(&TrialLicence,0,sizeof(SLS_LICENCE));
- TrialLicence.Type = SLS_TYPE_TIME_METER|SLS_TYPE_CONCURRENCY|SLS_TYPE_STANDALONE;
- TrialLicence.Meter=30; //30 day
- TrialLicence.CoUsers=1; //1 user only
- if(!m_pSheriff->License(TrialLicence))
- {
- ShowSheriffError();
- return FALSE;
- }
- ---------------------------------------------------------*/
- }
-
- if (!CheckLicence())
- return FALSE;
-
- // create main MDI Frame window
- CMainFrame* pMainFrame = new CMainFrame;
- if (!pMainFrame->LoadFrame(IDR_MAINFRAME))
- return FALSE;
- m_pMainWnd = pMainFrame;
-
- // The one and only window has been initialized, so show and update it.
- m_pMainWnd->ShowWindow(SW_SHOW);
- m_pMainWnd->UpdateWindow();
-
- return TRUE;
- }
-
- BOOL CSlsDemoApp::ExitInstance()
- {
- if(!m_bRunInDemoMode)
- {
- SLS_RELEASE Release;
- memset(&Release,0,sizeof(Release));
- Release.UnitsConsumed=0L;
- m_pSheriff->Release(Release);
- }
- delete m_pSheriff;
- m_pSheriff=NULL;
- return CWinApp::ExitInstance();
- }
-
- /////////////////////////////////////////////////////////////////////////////
- // CAboutDlg dialog used for App About
-
- class CAboutDlg : public CDialog
- {
- public:
- CAboutDlg();
-
- // Dialog Data
- //{{AFX_DATA(CAboutDlg)
- enum { IDD = IDD_ABOUTBOX };
- //}}AFX_DATA
-
- // ClassWizard generated virtual function overrides
- //{{AFX_VIRTUAL(CAboutDlg)
- protected:
- virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
- //}}AFX_VIRTUAL
-
- // Implementation
- protected:
- //{{AFX_MSG(CAboutDlg)
- // No message handlers
- //}}AFX_MSG
- DECLARE_MESSAGE_MAP()
- };
-
- CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
- {
- //{{AFX_DATA_INIT(CAboutDlg)
- //}}AFX_DATA_INIT
- }
-
- void CAboutDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CAboutDlg)
- //}}AFX_DATA_MAP
- }
-
- BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
- //{{AFX_MSG_MAP(CAboutDlg)
- // No message handlers
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
-
- // App command to run the dialog
- void CSlsDemoApp::OnAppAbout()
- {
- CAboutDlg aboutDlg;
- aboutDlg.DoModal();
- }
-
- /////////////////////////////////////////////////////////////////////////////
- // CSlsDemoApp commands
-
- BOOL CSlsDemoApp::CheckLicence()
- {
- SLS_REQUEST Request;
- SLS_PERMIT Permit;
- memset(&Request,0,sizeof(Request));
- Request.UnitsReserved=0L;
- if(m_pSheriff->Request(Request,Permit))
- {
- SLS_LICENCE_INFO LicenceInfo;
- m_pSheriff->QueryLicenceInfo(LicenceInfo);
-
- SLS_OPTIONS Options;
- Options.HeartbeatTime=2;
- Options.ReclaimTime=3;
- Options.Reserved1=0;
- Options.Reserved2=0;
- m_pSheriff->SetOptions(Options);
-
- m_bLicenceOK=TRUE;
- m_nAccessKey=Permit.AccessKey;
- return TRUE;
- }
- //Request Failure
- BOOL bRegister=FALSE;
- //Register product
- if(m_pSheriff->GetLastError()==SLS_E_LICENCE_UNREGISTERED)
- {
- bRegister=RegisterProduct();
- }
-
- if(m_pSheriff->GetLastError()==SLS_E_LICENCE_UNDEFINED || bRegister)
- {
- //Licence not registered yet
- CRegisterDlg dlgRegister;
- int nRet=dlgRegister.DoModal();
- if(nRet==IDC_REGISTER)
- return RegisterLicence();
- if(nRet==IDC_EXIT)
- return FALSE;
- if(nRet==IDC_CONTINUE)
- {
- RunInDemoMode();
- return TRUE;
- }
- }
- else
- if(m_pSheriff->GetLastError()==SLS_E_LICENCE_EXPIRED)
- {
- //Licence has expired
- CRenewDlg dlgRenew;
- int nRet=dlgRenew.DoModal();
- if(nRet==IDC_RENEW)
- return RegisterLicence();
- if(nRet==IDC_EXIT)
- return FALSE;
- if(nRet==IDC_CONTINUE)
- {
- RunInDemoMode();
- return TRUE;
- }
- }
- else
- if(m_pSheriff->GetLastError()==SLS_E_LICENCE_EXCEEDED)
- {
- //Too many users
- CExceedDlg dlgExceed;
- int nRet=dlgExceed.DoModal();
- if(nRet==IDC_EXIT)
- return FALSE;
- if(nRet==IDC_CONTINUE)
- {
- RunInDemoMode();
- return TRUE;
- }
- return FALSE;
- }
-
- //Other errors
- ShowSheriffError();
- return FALSE;
- }
-
- BOOL CSlsDemoApp::RegisterProduct()
- {
- //Register product
- char szModuleName[257],*p;
- HINSTANCE hinst = AfxGetInstanceHandle() ;
- GetModuleFileName(hinst,szModuleName,sizeof(szModuleName)-1);
- if(p=strrchr(szModuleName,'\\')) *p=0;
- if(!m_pSheriff->Register("Sheriff Demo",szModuleName))
- {
- ShowSheriffError();
- return FALSE;
- }
-
- return TRUE;
- }
-
- BOOL CSlsDemoApp::RegisterLicence()
- {
- //Get Reference Code
- CString strReference;
- if(!m_pSheriff->GetReference(strReference))
- {
- ShowSheriffError();
- return FALSE;
- }
-
- CLicenceDlg dlgLicence;
- dlgLicence.m_strReferenceCode=strReference;
- if(dlgLicence.DoModal()==IDOK)
- {
- if(!m_pSheriff->SetLicence(dlgLicence.m_strReferenceCode,dlgLicence.m_strLicenceKey))
- {
- ShowSheriffError();
- return FALSE;
- }
-
- SLS_REQUEST Request;
- SLS_PERMIT Permit;
- memset(&Request,0,sizeof(Request));
- Request.UnitsReserved=0L;
- if(!m_pSheriff->Request(Request,Permit))
- {
- ShowSheriffError();
- return FALSE;
- }
-
- m_bLicenceOK=TRUE;
- m_nAccessKey=Permit.AccessKey;
- return TRUE;
- }
- return FALSE;
- }
-
- void CSlsDemoApp::ShowSheriffError()
- {
- CString strError;
- m_pSheriff->GetLastErrorMessage(strError);
- AfxMessageBox(strError,MB_OK|MB_ICONSTOP);
- }
-
- void CSlsDemoApp::OnTestExport()
- {
- CExportDlg dlg;
- if(dlg.DoModal() == IDOK)
- {
- //We must release our current licence handle before export
- //We'll request again once finish exporting
- SLS_RELEASE Release;
- memset(&Release,0,sizeof(Release));
- Release.UnitsConsumed=0L;
- m_pSheriff->Release(Release);
-
- //for simplicity, the export parameters are fixed in the demo
- //you may want to provide options in the Export Dialog
- //for users to select, like what SLSADMIN does
- SLS_LICENCE ExportLicence;
- memset(&ExportLicence,0,sizeof(SLS_LICENCE));
- ExportLicence.Type = SLS_TYPE_TIME_METER|SLS_TYPE_CONCURRENCY|SLS_TYPE_STANDALONE;
- ExportLicence.Meter=10; //10 day
- ExportLicence.CoUsers=1; //1 user only
-
- CString strExportLicenceKey;
- if(!m_pSheriff->Export(ExportLicence,dlg.m_strReferenceCode,strExportLicenceKey))
- {
- ShowSheriffError();
- return;
- }
-
- MessageBox(NULL,strExportLicenceKey,"Export Key",MB_OK);
-
- //We must release the
- if(!CheckLicence())
- {
- //check licence failed, you might want to exit your application now
- //exit
- }
- }
- }
-
-
- void CSlsDemoApp::OnTestQueryuserinfo()
- {
- DWORD dwUserCount = m_pSheriff->GetUserCount();
- for(DWORD i=1;i<=dwUserCount;i++)
- {
- SLS_USER_INFO UserInfo;
- m_pSheriff->QueryUserInfo(i,UserInfo);
- }
- }
-